package com.beiwei.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beiwei.auto.mode.Daylog;
import com.beiwei.auto.mode.User;
import com.beiwei.auto.mode.Weekgroup;
import com.beiwei.auto.mode.Weeklog;
import com.beiwei.auto.mode.WeeklogExample;
import com.beiwei.mapper.VDaylogMapper;
import com.beiwei.mapper.VWeekgroupMapper;
import com.beiwei.mapper.VWeeklogMapper;
import com.beiwei.mode.VWeeklog;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class WeeklogService {
	@Autowired
	private VWeeklogMapper weeklogMapper;
	@Autowired
	private VDaylogMapper daylogMapper;
	@Autowired
	private VWeekgroupMapper weekgroupMapper;

	/**
	 * 根据用户的id查询该用户所有的周报
	 */
	public Page<Weeklog> findWeeklogListByUserId(Integer userId, Integer pageNum, Integer pageCount) throws Exception {
		WeeklogExample example = new WeeklogExample();
		WeeklogExample.Criteria criteria = example.createCriteria();
		criteria.andWeeklogUserIdEqualTo(userId);
		criteria.andDelFlagNotEqualTo(1);
		example.setOrderByClause("weeklog_id desc");
		
		// 获取第1页，10条内容，默认查询总数count
		PageHelper.startPage(pageNum, pageCount);
		Page<Weeklog> weeklogs = (Page<Weeklog>)weeklogMapper.selectByExample(example);
		
		return weeklogs;
	}

	/**
	 * 根据部门id和groupId查询出该部门本周的所有周报
	 */
	public List<VWeeklog> findVWeeklogListByDepartmentIdAndGroupId(Integer departmentId, Integer groupId) throws Exception {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("groupId", groupId);
		map.put("departmentId", departmentId);
		return weeklogMapper.findVWeeklogListByDepartmentIdAndGroupId(map);
	}

	/**
	 * 根据weeklogId查询出对应的VWeeklog
	 */
	public VWeeklog findVWeeklogByWeeklogId(Integer weeklogId) throws Exception {
		VWeeklog weeklog = weeklogMapper.findVWeeklogByWeeklogId(weeklogId);

		return weeklog;
	}

	/**
	 * 更改VWeeklog 更改周记并更改该周记下边的所有日报
	 */
	public boolean updateVWeeklog(VWeeklog weeklog) throws Exception {
		if (1 != weeklogMapper.updateByPrimaryKeySelective(weeklog))
			return false;
		if (null != weeklog.getDaylogs() && weeklog.getDaylogs().size() > 0) {
			for (Daylog daylog : weeklog.getDaylogs()) {
				if (1 != daylogMapper.updateByPrimaryKeySelective(daylog))
					return false;
			}
		}
		return true;

	}

	/**
	 * 给所有人都创建一份周报（包括7个日报）
	 */
	public boolean addCreateWeeklog(List<User> users) throws Exception {
		boolean result = true;

		// 1、先新建一个weekgroup
		Weekgroup weekgroup = new Weekgroup();
		result = (1 != weekgroupMapper.getWeekgroupIdByAddWeekgroup(weekgroup)) ? false : true;

		// 2、给每个人都创建一个weeklog
		Weeklog weeklog = new Weeklog();
		Daylog daylog = new Daylog();
		for (User user : users) {
			weeklog.setWeeklogUserId(user.getUserId());
			weeklog.setWeeklogGroupId(weekgroup.getGroupId());
			// 获取weeklogId
			result = (1 != weeklogMapper.getWeeklogIdByAddWeeklog(weeklog)) ? false : true;
			daylog.setDaylogWeeklogId(weeklog.getWeeklogId());
			
			// 3、给每个周报都添加7个日报
			for (int i = 0; i < 7; i++) {
				result = (1 != daylogMapper.insertSelective(daylog)) ? false : true;
			}

		}
		return result;
	}

}
